import Vue from "vue";
import HeyUI from 'heyui';
import VueRouter from "vue-router";
import product from './product/product';
import productCate from './product/productCate';
import productType from './product/productType';
import productArea from './product/productArea';
import file from './file/index';
import article from './article/article';
import articleCate from './article/articleCate';
import articleLabel from './article/articleLabel';
import set from './set';
import alert from './alert/index';
Vue.use(VueRouter);

const routes = [

  {
    path: "/login",
    name: "Login",
    meta: { isLogin: true },
    component: () => import("@/views/Login.vue"),
  },
  {
    path: "/",
    name: "Index",
    component: () => import("@/views/Layout.vue"),
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () => import("@/views/home/index.vue"),
      },
      ...product,
      ...productCate,
      ...productType,
      ...productArea,
      ...file,
      ...article,
      ...articleCate,
      ...articleLabel,
      ...set,
      ...alert,
      {
        // 进入后的404
        path: "*",
        name: "errorPage",
        component: () => import("@/components/exception/404.vue"),
      },
    ]

  },
  // 全局404
  {
    path: "*",
    name: "error",
    component: () => import("@/components/exception/404.vue"),
  },

];

const router = new VueRouter({
  linkActiveClass: 'active',
  // 左边menu的active  待定、
  routes
});

// 在当前地址下回到当前地址会报错的解决
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
router.beforeEach((to, from, next) => {
  // 由于没设置根目录 所有直接让他跳转到home
  HeyUI.$LoadingBar.start();
  if (!to.meta.isLogin && !localStorage.token) {
    return next('/login')
  }
  if (to.fullPath === '/') { return next('home') }
  next();
})
router.afterEach(() => {
  HeyUI.$LoadingBar.success();
});
export default router;
